package com.fmn.dots_final_i_hope;


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import model.FieldSize;
import model.GameField;
import model.Player;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;



public class OptionsActivity extends Activity {

	ImageView p1Image,p2Image;
	EditText p1NameInput,p2NameInput;
	Spinner fieldSizes,themes;
	ArrayList<FieldSize> gameSizes = new ArrayList<FieldSize>();
	ArrayList<String> themesList = new ArrayList<String>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_options);
		 initializeFields();
		setListeners();
	}


	private void initializeFields() {
		
		p1Image = (ImageView) findViewById(R.id.player1Image);
		 p2Image = (ImageView) findViewById(R.id.player2Image);
		 
		 p1Image.setImageResource(PlayerControl.p1.getChosenImage());
		 p2Image.setImageResource(PlayerControl.p2.getChosenImage());
		 
		 p1NameInput = (EditText) findViewById(R.id.player1NameET);
		 p2NameInput = (EditText) findViewById(R.id.player2NameET);
		 addValuesToGameSizeList(gameSizes);
		 
		 fieldSizes = (Spinner) findViewById(R.id.gameSizeSelectSpinner);
		 themes = (Spinner) findViewById(R.id.themeSelectSpinner);
		 
		 populateSpinnerFromCollection(fieldSizes,gameSizes);
		 themesList.add("Colorfull");
		 populateSpinnerFromCollection(themes,themesList);
		 fieldSizes.setOnItemSelectedListener(itemSelecedListener());
	}


	


	private OnItemSelectedListener itemSelecedListener() {
		
		return new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if(arg0.getItemAtPosition(arg2) instanceof FieldSize){
					FieldSize selectedSize = (FieldSize) arg0.getItemAtPosition(arg2);	
					switch (selectedSize) {
					case ThreeByThree:
						PlayerControl.choosenLayoutId = R.layout.field_3x3;
						PlayerControl.choosenCol = 3;
						PlayerControl.choosenRow = 3;
						break;
					case FourByThree:
						PlayerControl.choosenLayoutId = R.layout.field_4x3;
						PlayerControl.choosenCol = 4;
						PlayerControl.choosenRow = 3;
						break;
					case FourByFour:
						PlayerControl.choosenLayoutId = R.layout.field_4x4;
						PlayerControl.choosenCol = 4;
						PlayerControl.choosenRow = 4;
						break;
					case FiveByFour:
						PlayerControl.choosenLayoutId = R.layout.field_5x4;
						PlayerControl.choosenCol = 5;
						PlayerControl.choosenRow = 4;
						break;
					case FiveByFive:
						PlayerControl.choosenLayoutId = R.layout.field_5x5;
						PlayerControl.choosenCol = 5;
						PlayerControl.choosenRow = 5;
						break;
					default:
						PlayerControl.choosenLayoutId = R.layout.field_3x3;
						PlayerControl.choosenCol = 3;
						PlayerControl.choosenRow = 3;
						break;
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		};
	}


	private void addValuesToGameSizeList(Collection<FieldSize> collection) {
		collection.add(FieldSize.ThreeByThree);
		collection.add(FieldSize.FourByThree);
		collection.add(FieldSize.FourByFour);
		collection.add(FieldSize.FiveByFour);
		collection.add(FieldSize.FiveByFive);
	}


	private void populateSpinnerFromCollection(Spinner spinner, List<FieldSize> collection) {
		ArrayAdapter<FieldSize> spinnerArrayAdapter = new ArrayAdapter<FieldSize>(this, android.R.layout.simple_spinner_item, collection);
		spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(spinnerArrayAdapter);
	}

	//TEMP METHOD DELETE ME LATER - WHEN IMPLEMENT THEMES.
	private void populateSpinnerFromCollection(Spinner spinner,ArrayList<String> collection) {
		ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, collection);
		spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(spinnerArrayAdapter);
		
	}

	private void setListeners() {
		p1Image.setOnClickListener(imageClickListener(PlayerControl.p1));
		p2Image.setOnClickListener(imageClickListener(PlayerControl.p2));
		
		p1NameInput.addTextChangedListener(editTextChangeListener(PlayerControl.p1));
		p2NameInput.addTextChangedListener(editTextChangeListener(PlayerControl.p2));
	}


	private TextWatcher editTextChangeListener(final Player player) {
		
		return new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.length()>0 && s!=null)
					player.setName(s.toString());
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		};
	}


	private OnClickListener imageClickListener(final Player player) {
		return new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				GameDialogs.showPlayerColorDialog(OptionsActivity.this, player);
				if(v instanceof ImageView){
//					Drawable drawable = getResources().getDrawable(player.getChosenImage());
					ImageView myImage = (ImageView)v;
					myImage.setImageResource(player.getChosenImage());
//					myImage.invalidate();
//					redrawView(myImage);
				}
			}
			
		};
	}
	
//	private void redrawView(View view){
//		view.invalidate();
//		View v = findViewById(R.id.optionsLayout);
//		v.invalidate();
//	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		Toast.makeText(this, "Settings are saved!", Toast.LENGTH_SHORT).show();
	}
}
